﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using CommDll;

namespace InstitutionAssitant
{
    /// <summary>
    /// ClassName:frmPasswordChange
    /// Version:1.0
    /// Date:2019-05-23
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为实现用口令修改
    /// </remarks>    
    public partial class frmPasswordChange : DevComponents.DotNetBar.OfficeForm
    {
        #region 01.自定义属性
        bool m_bIsOK = false;
        #endregion

        public frmPasswordChange()
        {
            InitializeComponent(); 
        }

        #region 01.系统事件方法
        private void frmPasswordChange_FormClosing(object sender, FormClosingEventArgs e)
        {
            // 未更新口令时进行提示
            if (!m_bIsOK)
            {
                DialogResult result = CommFun._messageBoxShow("确定放弃修改用户口令？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (result != DialogResult.OK)
                {
                    e.Cancel = true;
                }
            }
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //00 数据合法性验证
            if (txtPassword0.Text.Trim().Length < 1)
            {
                CommFun._messageBoxShow("原口令不能为空！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (txtPassword1.Text.Trim().Length < 1)
            {
                CommFun._messageBoxShow("新口令不能为空！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (txtPassword2.Text.Trim().Length < 1)
            {
                CommFun._messageBoxShow("口令确认不能为空！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            // 01 新口令重复性校验
            if (txtPassword1.Text.Trim() != txtPassword2.Text.Trim()){
                CommFun._messageBoxShow("新口令不一致，请重新确认新口令！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            // 02 原口令校验并更新口令
            string strTemp = FunctionComm._encryptMD5(txtPassword0.Text.Trim());
            if (AppInfo.m_UserInfo.m_strPassword == strTemp)
            {
                // 接口发送数据          
                m_bIsOK = AppInfo.m_UserInfo._changePassword(FunctionComm._encryptMD5(txtPassword1.Text.Trim()));
                if (m_bIsOK)
                {
                    AppInfo.m_UserInfo.m_strPassword = FunctionComm._encryptMD5(txtPassword1.Text.Trim());
                    CommFun._messageBoxShow("用户口令修改成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else {
                    CommFun._messageBoxShow("口令修改失败，请与管理员联系！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else {
                CommFun._messageBoxShow("原口令不正确，请重新确定！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion
    }
}